package herramientas;

import java.awt.Component;

import javax.swing.JTable;
import javax.swing.table.TableCellRenderer;
import javax.swing.table.TableColumn;
import javax.swing.table.TableColumnModel;

public class ColumnResizer {
	public static void adjustColumnPreferredWidths(JTable table) {
	// strategy - get max width for cells in column and
	// make that the preferred width
	TableColumnModel columnModel = table.getColumnModel();
	for (int col=0; col<table.getColumnCount(); col++) {
	int maxwidth = 200;
	for (int row=0; row<table.getRowCount(); row++) {
	TableCellRenderer rend =
	
	table.getCellRenderer(row, col);
	Object value = table.getValueAt (row, col);
	Component comp =
	rend.getTableCellRendererComponent (table,
	value,
	false,
	false,
	row,
	col);
	maxwidth = Math.max (comp.getPreferredSize().width, maxwidth);
	} // for row
	TableColumn column = columnModel.getColumn (col);
	column.setPreferredWidth (maxwidth);
	} // for col
	}
	}